<!--{template lyear:header_simple_start}-->
<script src="admin/scripts/admin.js?{VERHASH}"></script>
<!--{template lyear:header_simple_end}-->
<main class="bs-main-container">
	<div class="container-fluid">
		<div class="card">
			<div class="card-body">
				<ul class="nav nav-pills">
					<li class="nav-item">
						<a class="nav-link <!--{if $operation=='export'}-->active<!--{/if}-->" hidefocus="true" href="{MOD_URL}&op=database&operation=export">{lang export}</a>
					</li>
					<li class="nav-item">
						<a class="nav-link <!--{if $operation=='import'}-->active<!--{/if}-->" hidefocus="true" href="{MOD_URL}&op=database&operation=import">{lang nav_db_import}</a>
					</li>
					<li class="nav-item">
						<a class="nav-link <!--{if $operation=='runquery'}-->active<!--{/if}-->" hidefocus="true" href="{MOD_URL}&op=database&operation=runquery">{lang nav_db_runquery}</a>
					</li>
					<li class="nav-item">
						<a class="nav-link <!--{if $operation=='db'}-->active<!--{/if}-->" hidefocus="true" href="{MOD_URL}&op=database&operation=db">{lang database_db_switch}</a>
					</li>
					<li class="nav-item">
						<a class="nav-link <!--{if $operation=='utf8mb4'}-->active<!--{/if}-->" hidefocus="true" href="{MOD_URL}&op=database&operation=utf8mb4">{lang database_db_utf8mb4}</a>
					</li>
				</ul>
			</div>
		</div>
		<div class="card">
			<div class="card-body">
				<!--{if $operation=='export'}-->
				<div class="alert alert-warning">
					<h5>{lang board_message}</h5>
					<ul class="form-text">
						{lang db_export_tips}
					</ul>
				</div>
				<!--{if !$submit}-->
				<form id="cpform" action="{MOD_URL}&op=database&operation=export&setup=1" class="form-horizontal" method="post" name="cpform">
					<input type="hidden" value="{FORMHASH}" name="formhash">
					<input type="hidden" value="true" name="exportsubmit">
					<div class="row mb-3">
						<label class="col-sm-2">{lang db_export_type}</label>
						<div class="col-sm-10">
							<div class="form-check form-check-inline">
								<input class="form-check-input" type="radio" id="type1" name="type" value="dzz" checked="" onclick="document.getElementById('showtables').style.display = 'none';">
								<label class="form-check-label" for="type1">{lang all_data_table}</label>
							</div>
							<div class="form-check form-check-inline">
								<input class="form-check-input" type="radio" id="type2" name="type" value="custom" onclick="document.getElementById('showtables').style.display = '';">
								<label class="form-check-label" for="type2">{lang db_export_custom}</label>
							</div>
						</div>
						<div id="showtables" style="display:none;">
							<div class="form-check">
								<input class="form-check-input" type="checkbox" class="form-check-input" name="chkall" onclick="checkAll('prefix', this.form, 'customtables', 'chkall', true)" checked="checked" type="checkbox" id="chkalltables">
								<label class="form-check-label" for="chkalltables">{lang check_all_data_table}</label>
							</div>
							<div class="item">
								<!--{loop $dztables $value}-->
								<div class="form-check form-check-inline col-6 col-sm-4">
									<input class="form-check-input" type="checkbox" class="form-check-input" name="customtables[]" value="$value" id="$value" checked="checked">
									<label class="form-check-label" for="$value">$value</label>
								</div>
								<!--{/loop}-->
							</div>
						</div>
					</div>
					<div id="advanceoption" style="display:none">
						<div class="row mb-3">
							<label class="col-sm-2">{lang db_export_method}</label>
							<div class="col-sm-10">
								<div class="form-check form-check-inline">
									<input type="radio" class="form-check-input" name="method" value="shell" onclick="if('0') {if(this.form.sqlcompat[2].checked==true) this.form.sqlcompat[0].checked=true; this.form.sqlcompat[2].disabled=true; this.form.sizelimit.disabled=true;} else {this.form.sqlcharset[0].checked=true; for(var i=1; i&lt;=5; i++) {if(this.form.sqlcharset[i]) this.form.sqlcharset[i].disabled=true;}}" id="method_shell" $shelldisabled>
									<label class="form-check-label" for="method_shell">{lang db_export_shell}</label>
								</div>
								<div class="form-check form-check-inline">
									<input type="radio" class="form-check-input" name="method" value="multivol" checked="checked" onclick="this.form.sqlcompat[2].disabled=false; this.form.sizelimit.disabled=false; for(var i=1; i<=5; i++) {if(this.form.sqlcharset[i]) this.form.sqlcharset[i].disabled=false;}" id="method_multivol">
									<label class="form-check-label" for="method_multivol">{lang db_export_multivol}</label>
								</div>
								<div class="form-check form-check-inline">
									<input type="text" class="form-control" name="sizelimit" value="2048">
								</div>
							</div>
						</div>
						<div class="row mb-3">
							<label class="col-sm-2">{lang db_export_options_extended_insert}</label>
							<div class="col-sm-10">
								<div class="form-check form-check-inline">
									<input type="radio" class="form-check-input" name="extendins" value="1" id="extendins1">
									<label class="form-check-label" for="extendins1">{lang yes}</label>
								</div>
								<div class="form-check form-check-inline">
									<input type="radio" class="form-check-input" name="extendins" value="0" checked="checked" id="extendins2">
									<label class="form-check-label" for="extendins2">{lang no}</label>
								</div>
							</div>
						</div>
						<div class="row mb-3">
							<label class="col-sm-2">{lang db_export_options_sql_compatible}</label>
							<div class="col-sm-10">
								<div class="form-check form-check-inline">
									<input type="radio" class="form-check-input" name="sqlcompat" value="" checked="" id="sqlcompat1">
									<label class="form-check-label" for="sqlcompat1">{lang default}</label>
								</div>
								<div class="form-check form-check-inline">
									<input type="radio" class="form-check-input" name="sqlcompat" value="MYSQL40" id="sqlcompat2">
									<label class="form-check-label" for="sqlcompat2">MySQL 3.23/4.0.x</label>
								</div>
								<div class="form-check form-check-inline">
									<input type="radio" class="form-check-input" name="sqlcompat" value="MYSQL41" disabled="" id="sqlcompat3">
									<label class="form-check-label" for="sqlcompat3">MySQL 4.1.x/5.x</label>
								</div>
							</div>
						</div>
						<div class="row mb-3">
							<label class="col-sm-2">{lang db_export_options_charset}</label>
							<div class="col-sm-10">
								<div class="form-check form-check-inline">
									<input type="radio" class="form-check-input" name="sqlcharset" value="" id="sqlcharset1">
									<label class="form-check-label" for="sqlcharset1">{lang db_export_options_charset}</label>
								</div>
								<div class="form-check form-check-inline">
									<input type="radio" class="form-check-input" name="sqlcharset" value="utf8" id="sqlcharset2">
									<label class="form-check-label" for="sqlcharset2">UTF8</label>
								</div>
							</div>
						</div>
						<div class="row mb-3">
							<label class="col-sm-2">{lang db_export_usehex}</label>
							<div class="col-sm-10">
								<div class="form-check form-check-inline">
									<input type="radio" class="form-check-input" name="usehex" value="1" checked="checked" id="usehex1">
									<label class="form-check-label" for="usehex1">{lang yes}</label>
								</div>
								<div class="form-check form-check-inline">
									<input type="radio" class="form-check-input" name="usehex" value="1" checked="checked" id="usehex2">
									<label class="form-check-label" for="usehex2">{lang no}</label>
								</div>
							</div>
						</div>
						<div class="row mb-3">
							<label class="col-sm-2">{lang db_export_usezip}</label>
							<div class="col-sm-10">
								<div class="form-check form-check-inline">
									<input type="radio" class="form-check-input" name="usezip" value="1" id="usezip1">
									<label class="form-check-label" for="usezip1">{lang db_export_zip_1}</label>
								</div>
								<div class="form-check form-check-inline">
									<input type="radio" class="form-check-input" name="usezip" value="2" id="usezip2">
									<label class="form-check-label" for="usezip2">{lang db_export_zip_2}</label>
								</div>
								<div class="form-check form-check-inline">
									<input type="radio" class="form-check-input" name="usezip" value="0" checked id="usezip3">
									<label class="form-check-label" for="usezip3">{lang db_export_zip_3}</label>
								</div>
							</div>
						</div>
						<div class="row mb-3">
							<label class="col-sm-2">{lang db_export_filename}</label>
							<div class="col-sm-10">
								<input type="text" class="form-control" class="form-check-input" name="filename" value="$defaultfilename">
							</div>
						</div>
					</div>
					<dl class="mb-3 d-grid">
						<button type="submit" class="btn btn-primary btn-round bodyloading" name="exportsubmit" value="true" >{lang blank_submit}</button>
					</dl>
					<div class="form-check">
						<input type="checkbox" class="form-check-input" value="1" onclick="document.getElementById('advanceoption').style.display = document.getElementById('advanceoption').style.display == 'none' ? '' : 'none'; this.value = this.value == 1 ? 0 : 1; this.checked = this.value == 1 ? false : true" id="btn_more">
						<label class="form-check-label" for="btn_more">{lang more_options}</label>
					</div>
				</form>
				<!--{else}-->
				<!--{if $msg}-->
				<div class="alert alert-{$msg_type}" role="alert">
					$msg
				</div>
				<!--{/if}-->
				<!--{if $redirecturl}-->
				<dl class="mb-3 d-grid">
					<a href="{$redirecturl}" class="btn btn-primary">{lang message_redirect}</a>
				</dl>
				<script type="text/JavaScript">setTimeout(function(){location.href='{$redirecturl}';}, 2000);</script>
				<!--{/if}-->
				<!--{/if}-->
				<!--{elseif $operation=='import'}-->
				<!--{if $msg}-->
				<div class="alert alert-{$msg_type}" role="alert">
					$msg
				</div>
				<!--{if $redirecturl}-->
				<dl class="mb-3 d-grid">
					<a href="{$redirecturl}" class="btn btn-primary">{lang message_redirect}</a>
				</dl>
				<script type="text/JavaScript">setTimeout(function(){location.href='{$redirecturl}';}, 2000);</script>
				<!--{/if}-->
				<!--{else}-->
				<div class="alert alert-warning">
					<h5>{lang board_message}</h5>
					<ul class="form-text">
						{lang db_import_tips}
					</ul>
				</div>
				$do_import_option
				<form id="cpform" action="{MOD_URL}&op=database&operation=import" class="form-horizontal" method="post" name="cpform">
					<input type="hidden" value="{FORMHASH}" name="formhash">
					<input type="hidden" value="true" name="deletesubmit">
					<div class="table-responsive">
						<table class="table table-hover align-middle" style="border-top:1px solid #DDD">
							<thead>
								<th></th>
								<th>{lang filename}</th>
								<th>{lang version}</th>
								<th>{lang time}</th>
								<th>{lang type}</th>
								<th>{lang big_small}</th>
								<th>{lang db_method}</th>
								<th>{lang db_volume}</th>
								<th></th>
							</thead>
							<!--{loop $list $key $val}-->
							<tr>
								<td><input type="checkbox" class="form-check-input" name="delete[]" value="$key"></td>
								<td>
									<!--{if $val['list']}-->
									<a href="javascript:;" onclick="jQuery('#exportlog_{$key}').toggle()">$key</a>
									<!--{else}-->
									<a href="$val[filename]">$key</a>
									<!--{/if}-->
								</td>
								<td>$val[version]</td>
								<td>$val['dateline']</td>
								<td>$val[ftype]</td>
								<td>$val[size]</td>
								<td>$val[method]</td>
								<td>$val[volume]</td>
								<td>
									<!--{if $val['list']}-->
									<a href="{$datasiteurl}restore.php?operation=import&from=server&datafile_server=$val[datafile_server]&importsubmit=yes" class="btn btn-outline-primary" <!--{if $info[ 'version'] !=$_G[ 'setting'][ 'version']}--> onclick="return confirm('{lang db_import_confirm}');"<!--{else}-->onclick="return confirm('{lang db_import_confirm_sql}');"<!--{/if}-->target="_blank">{lang import}</a>
									<!--{else}-->
									<a href="{$datasiteurl}restore.php?operation=importzip&datafile_server=$info[datafile_server]&importsubmit=yes" class="btn btn-outline-primary" onclick="return confirm('{lang db_import_confirm_zip}');" target="_blank">{lang db_import_unzip}</a>
									<!--{/if}-->
								</td>
							</tr>
							<thead id="exportlog_{$key}" style="display:none;">
								<!--{loop $val[list] $key1 $val1}-->
								<tr>
									<td></td>
									<td>
										<a href="$val1[filename]">$val1[filename]</a>
									</td>
									<td>$val1[version]</td>
									<td>$val1['dateline']</td>
									<td></td>
									<td>$val1[size]</td>
									<td></td>
									<td>$val1[volume]</td>
									<td></td>
								</tr>
								<!--{/loop}-->
							</thead>
							<!--{/loop}-->
						</table>
					</div>
					<div class="col-12 mb-2">
						<div class="form-check">
							<input type="checkbox" class="form-check-input" name="chkall" id="chkallspKI" onclick="checkAll('prefix', this.form, 'delete')">
							<label class="form-check-label" for="chkallspKI">{lang del}</label>
						</div>
					</div>
					<div class="col-12">
						<button type="submit" class="btn btn-primary btn-round bodyloading" name="exportsubmit" value="true" >{lang blank_submit}</button>
					</div>
				</form>
				<!--{/if}-->
				<!--{elseif $operation=='runquery'}-->
				<div class="alert alert-warning">
					<h5>{lang board_message}</h5>
					<ul class="form-text">
						{lang db_runquery_tips}
					</ul>
				</div>
				<!--{if $msg}-->
				<div class="alert alert-{$msg_type}" role="alert">
					$msg
				</div>
				<!--{if $redirecturl}-->
				<dl class="mb-3 d-grid">
					<a href="{$redirecturl}" class="btn btn-primary">{lang message_redirect}</a>
				</dl>
				<script type="text/JavaScript">setTimeout(function(){location.href='{$redirecturl}';}, 5000);</script>
				<!--{/if}-->
				<!--{else}-->
				<form id="cpform" action="{MOD_URL}&op=database&operation=runquery" method="post" name="cpform">
					<input type="hidden" value="{FORMHASH}" name="formhash">
					<input type="hidden" value="true" name="sqlsubmit">
					<div class="mb-3">
						<label for="queries" class="form-label">{lang db_runquery_sql}</label>
						<textarea cols="85" rows="10" name="queries" id="queries" class="form-control"></textarea>
					 </div>
					 <div class="col-12">
						<div class="form-check">
						 <input class="form-check-input" type="checkbox" id="createcompatible" name="createcompatible" value="1" checked="checked">
						 <label class="form-check-label" for="createcompatible">
							{lang db_runquery_createcompatible}
						 </label>
						</div>
					 </div>
					<dl class="mb-3 d-grid">
						<button type="submit" class="btn btn-primary btn-round bodyloading">{lang blank_submit}</button>
					</dl>
				</form>
				<!--{/if}-->
				<!--{elseif $operation=='db'}-->
				<div class="alert alert-info" role="alert">
					<h5>数据库驱动配置说明</h5>
					<p class="mb-2">当前系统支持 <code>mysqli</code> 和 <code>pdo</code> 两种数据库驱动，若需切换驱动（如启用 PDO），请手动修改配置文件(/config/config.php)：</p>
					<p class="mb-1">在 <code>{eval echo '$_config["db"]';}</code> 数组下，新增或修改 <code>driver</code> 字段（默认为空，对应 mysqli）：</p>
					<pre class="bg-dark text-light p-2 rounded overflow-auto">
						<code>// 数据库驱动配置（二选一）
						// 1. 启用 PDO 驱动（推荐，支持参数化查询，更安全）<br>
						{eval echo '$_config["db"]["driver"] = "pdo";';}<br>
						// 2. 启用 mysqli 驱动（默认方式）<br>
						{eval echo '$_config["db"]["driver"] = "mysqli";';}
						</code>
					</pre>
					<div class="mb-3">
						<span class="fw-bold">当前配置文件中的数据库驱动为：</span>
						{if $_config["db"]["driver"]}$_config["db"]["driver"]{else}未配置，使用默认方式{/if}
					</div>
					<div class="mb-3">
						<span class="fw-bold">当前服务器PDO支持状态：</span>
						{if $is_pdo_supported}<span class="text-success">✅ 已支持</span>{else}<span class="text-danger">❌ 未支持</span>{/if}
					</div>
					<div class="mb-3">
						<span class="fw-bold">当前服务器使用的数据库驱动为：</span>$_G['db_driver']
					</div>
					<div class="mb-0">注意事项：
						<ul class="mb-0">
							<li>修改配置后无需重启服务，刷新页面即可生效。</li>
							<li>建议修改前备份 <code>config.php</code> 文件，避免配置错误导致系统异常。</li>
						</ul>
					</div>
				</div>
				<!--{elseif $operation=='utf8mb4'}-->
				<form id="cpform" action="{MOD_URL}&op=database&operation=utf8mb4" method="post" name="cpform">
					<input type="hidden" value="{FORMHASH}" name="formhash">
					<input type="hidden" value="true" name="utf8mb4submit">
					<!--{if $_config['db']['common']['engine'] !== 'innodb'}-->
					<div class="alert alert-warning">
						请注意当前配置文件中的数据库引擎不是InnoDB，请先修改配置文件(/config/config.php)，新增或修改{eval echo '$_config["db"]["common"]["engine"]';}为<code>innodb</code>。
					</div>
					<!--{/if}-->
					<!--{if $_config['db']['1']['dbcharset'] !== 'utf8mb4'}-->
					<div class="alert alert-warning">
						请注意当前配置文件中的字符集校对不是utf8mb4，请先修改配置文件(/config/config.php)，修改{eval echo '$_config["db"]["1"]["dbcharset"]';}为<code>utf8mb4</code>。
					</div>
					<!--{/if}-->
					<div class="alert alert-info">
						<strong>数据库字符集升级说明</strong>
						<p>为提升系统对表情符号、生僻字等特殊内容的支持能力，现提供 <strong>"InnoDB + utf8mb4"</strong> 数据库字符集升级选项。若数据量较大（如超过 1GB / 含百万级数据表），推荐优先通过数据库工具手动升级，规避超时风险。</p>
						
						<strong>升级优势：</strong>
						<ul>
							<li><strong>完整字符支持</strong>：utf8mb4 是 MySQL 的完整 UTF-8 实现，可存储 😊🎉 等表情及生僻字，解决原字符集存储异常问题。</li>
							<li><strong>引擎性能优化</strong>：InnoDB 支持事务、行级锁，相比原引擎（MyISAM）大幅提升并发操作稳定性，减少数据丢失风险。</li>
							<li><strong>标准兼容</strong>：符合现代 Web 应用的字符编码标准，后续集成插件、扩展功能时兼容性更强。</li>
						</ul>
						
						<strong>注意事项：</strong>
						<ul class="mb-0">
							<li><strong>备份要求</strong>：升级前请务必备份所有文件和数据库数据。</li>
							<li><strong>时间消耗</strong>：升级过程根据数据量大小可能需要数分钟，请耐心等待</li>
							<li><strong>插件兼容</strong>：升级后部分插件可能不兼容（如报错），可联系插件作者获取适配版本</li>
							<li><strong>不可逆性</strong>：字符集与引擎升级后无法直接回退，需通过升级前的备份恢复原始状态，请务必确认备份完整后再操作。</li>
							<li><strong>超时风险</strong>：若通过本功能进行升级，可能触发两类超时：
								<ul class="mb-0">
									<li>1.PHP 脚本超时：默认 30-60 秒，大数据表（如百万级数据）执行引擎 / 字符集转换时易超时，中断升级。</li>
									<li>2.Nginx 连接超时：默认 60 秒，长时间无响应会断开连接，导致前端报错但后台仍可能运行，引发数据不一致。</li>
								</ul>
							</li>
						</ul>
					</div>
					 <!--{if $issubmit}-->
					 <div class="row mb-3">
						<div class="col-md-4">
							<div class="alert alert-info">
							<strong>总表数:</strong> {$count}
							</div>
						</div>
						<div class="col-md-4">
							<div class="alert alert-success">
							<strong>成功处理:</strong> {$success_count}
							</div>
						</div>
						<div class="col-md-4">
							<div class="alert alert-danger">
							<strong>处理失败:</strong> {$error_count}
							</div>
						</div>
					</div>
					<!--{if $error_count > 0}-->
					<div class="alert alert-danger">
						<strong>注意：</strong>存在错误或不支持的表，请检查后手动处理。
					</div>
					<!--{/if}-->
					<div class="table-responsive">
						<table class="table table-bordered table-hover align-middle">
							<thead class="thead-dark">
								<tr>
									<th>表名</th>
									<th>当前引擎</th>
									<th>字符集校对</th>
									<th>引擎状态</th>
									<th>字符集状态</th>
								</tr>
							</thead>
							<tbody>
								$html
							</tbody>
						</table>
					</div>
					<!--{/if}-->
					<dl class="mb-3 d-grid">
						<button type="submit" class="btn btn-primary btn-round bodyloading">开始升级</button>
					</dl>
				</form>
				<!--{/if}-->
			</div>
		</div>
	</div>
</main>
<!--{template lyear:footer_simple}-->